﻿using System;
using System.Drawing;
using Company.BLL.DuLieuChuan;
using Company.BLL.GC;
using Company.BLL.SXXK.ToKhai;
using Company.Interface.GC;
using Company.Interface.SXXK;
using Janus.Windows.GridEX;
using Janus.Windows.GridEX.EditControls;

namespace Company.Interface.SXXK
{
    public partial class HangMauDichDieuChinhForm : BaseForm
    {
        //-----------------------------------------------------------------------------------------
        private decimal clg;
        private decimal dgnt;
        public HopDong HD = new HopDong();
        public HangMauDichDieuChinhCollection HMDCollection = new HangMauDichDieuChinhCollection();
        public HangMauDichDieuChinh HMD = null;
        public string LoaiHangHoa;
        private decimal luong;
        public ToKhaiMauDich tokhai;
        public string MaNguyenTe;
        public string MaNuocXX;
        public string NhomLoaiHinh = string.Empty;
        private decimal st_clg;

        //-----------------------------------------------------------------------------------------				
        private decimal tgnt;
        private decimal tgtt_gtgt;

        private decimal tgtt_nk;
        private decimal tgtt_ttdb;
        private decimal tl_clg;
        private decimal tongTGKB;
        private decimal ts_gtgt;

        private decimal ts_nk;
        private decimal ts_ttdb;
        private decimal tt_gtgt;

        private decimal tt_nk;
        private decimal tt_ttdb;
        public decimal TyGiaTT;
        //-----------------------------------------------------------------------------------------

        public HangMauDichDieuChinhForm()
        {
            InitializeComponent();
        }

        private void khoitao_GiaoDien()
        {
            
        }

        private void khoitao_DuLieuChuan()
        {
            // Nước XX.
            this._Nuoc = Nuoc.SelectAll();

            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();
            cbDonViTinh.DataSource = this._DonViTinh;
            cbDonViTinh.SelectedValue = GlobalSettings.DVT_MAC_DINH;
        }

        private decimal tinhthue()
        {
            this.tgnt = this.dgnt*this.luong;
            this.tgtt_nk = this.tgnt*this.TyGiaTT;
            this.tt_nk = this.tgtt_nk*this.ts_nk;

            this.tgtt_ttdb = this.tgtt_nk + this.tt_nk;
            this.tt_ttdb = this.tgtt_ttdb*ts_ttdb;

            this.tgtt_gtgt = this.tgtt_ttdb + this.tt_ttdb;
            this.tt_gtgt = this.tgtt_gtgt*this.ts_gtgt;

            this.clg = this.tgtt_nk;
            this.st_clg = this.clg*this.tl_clg;

            txtTGNT.Value = this.tgnt;
            txtTGTT_NK.Value = this.tgtt_nk;
            txtTGTT_TTDB.Value = this.tgtt_ttdb;
            txtTGTT_GTGT.Value = this.tgtt_gtgt;
            txtCLG.Value = this.clg;

            txtTienThue_NK.Value = this.tt_nk;
            txtTienThue_TTDB.Value = this.tt_ttdb;
            txtTienThue_GTGT.Value = this.tt_gtgt;
            txtTien_CLG.Value = this.st_clg;

            txtTongSoTienThue.Value = this.tt_nk + this.tt_ttdb + this.tt_gtgt + this.st_clg;

            return this.tt_nk + this.tt_ttdb + this.tt_gtgt + this.st_clg;
        }

        //-----------------------------------------------------------------------------------------
        private bool checkMaHangExit(string maHang)
        {
            foreach (HangMauDichDieuChinh hmd in HMDCollection)
            {
                if (hmd.MaHang == maHang) return true;
            }
            return false;
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
           
        }

        //-----------------------------------------------------------------------------------------
        private void txtMaNPL_Leave(object sender, EventArgs e)
        {
            //this.fillNguyenPhuLieu(txtMaNPL.Text);
        }

        private void HangMauDichForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();
            this.khoitao_GiaoDien();
            TyGiaTT = tokhai.TyGiaTinhThue;
            dgList.DataSource = HMDCollection;
            dgList.Refetch();
            txtLanDC.Text = HMDCollection[0].LanDieuChinh.ToString();
        }

        private void txtLuong_Leave(object sender, EventArgs e)
        {
            //this.luong = Convert.ToDecimal(txtLuong.Value);
            //this.tinhthue();
        }

        private void txtDGNT_Leave(object sender, EventArgs e)
        {
            //this.dgnt = Convert.ToDecimal(txtDGNT.Value);
            //this.tinhthue();
        }

        private void txtTS_NK_Leave(object sender, EventArgs e)
        {
            //this.ts_nk = Convert.ToDecimal(txtTS_NK.Value)/100;
            //this.tinhthue();
        }

        private void txtTS_TTDB_Leave(object sender, EventArgs e)
        {
            //this.ts_ttdb = Convert.ToDecimal(txtTS_TTDB.Value)/100;
            //this.tinhthue();
        }

        private void txtTS_GTGT_Leave(object sender, EventArgs e)
        {
            //this.ts_gtgt = Convert.ToDecimal(txtTS_GTGT.Value)/100;
            //this.tinhthue();
        }

        private void txtTS_CLG_Leave(object sender, EventArgs e)
        {
            //this.tl_clg = Convert.ToDecimal(txtTL_CLG.Value)/100;
            //this.tinhthue();
        }

        //-----------------------------------------------------------------------------------------------
     

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells["NuocXX_ID"].Text = this.Nuoc_GetName(e.Row.Cells["NuocXX_ID"].Value.ToString());
            e.Row.Cells["DVT_ID"].Text = this.DonViTinh_GetName(e.Row.Cells["DVT_ID"].Text);
        }

        private void chkMienThue_CheckedChanged(object sender, EventArgs e)
        {
            //grbThue.Enabled = !chkMienThue.Checked;
            //txtTS_NK.Value = 0;
            //txtTS_TTDB.Value = 0;
            //txtTS_GTGT.Value = 0;
            //txtTL_CLG.Value = 0;
            //this.tinhthue();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMaHang_ButtonClick(object sender, EventArgs e)
        {
            switch (this.NhomLoaiHinh)
            {
                case "NSX":
                    NguyenPhuLieuRegistedForm f = new NguyenPhuLieuRegistedForm();
                    f.CalledForm = "HangMauDichForm";
                    f.MaHaiQuan = this.MaHaiQuan;
                    f.ShowDialog();
                    if (f.NguyenPhuLieuSelected.Ma != "")
                    {
                        txtMaHang.Text = f.NguyenPhuLieuSelected.Ma;
                        txtTenHang.Text = f.NguyenPhuLieuSelected.Ten;
                        txtMaHS.Text = f.NguyenPhuLieuSelected.MaHS;
                        cbDonViTinh.SelectedValue = f.NguyenPhuLieuSelected.DVT_ID.PadRight(3);
                    }
                    break;
                case "XSX":
                    if (this.LoaiHangHoa=="N")
                    {
                        NguyenPhuLieuRegistedForm fNPLX = new NguyenPhuLieuRegistedForm();
                        fNPLX.CalledForm = "HangMauDichForm";
                        fNPLX.MaHaiQuan = this.MaHaiQuan;
                        fNPLX.ShowDialog();
                        if (fNPLX.NguyenPhuLieuSelected.Ma != "")
                        {
                            txtMaHang.Text = fNPLX.NguyenPhuLieuSelected.Ma;
                            txtTenHang.Text = fNPLX.NguyenPhuLieuSelected.Ten;
                            txtMaHS.Text = fNPLX.NguyenPhuLieuSelected.MaHS;
                            cbDonViTinh.SelectedValue = fNPLX.NguyenPhuLieuSelected.DVT_ID.PadRight(3);
                        }
                    }
                    else
                    {
                        SanPhamRegistedForm fSPX = new SanPhamRegistedForm();
                        fSPX.CalledForm = "HangMauDichForm";
                        fSPX.MaHaiQuan = this.MaHaiQuan;
                        fSPX.ShowDialog();
                        if (fSPX.SanPhamSelected.Ma != "")
                        {
                            txtMaHang.Text = fSPX.SanPhamSelected.Ma;
                            txtTenHang.Text = fSPX.SanPhamSelected.Ten;
                            txtMaHS.Text = fSPX.SanPhamSelected.MaHS;
                            cbDonViTinh.SelectedValue = fSPX.SanPhamSelected.DVT_ID.PadRight(3);
                        }
                    }
                    break;
                case "NGC":
                    if (this.LoaiHangHoa=="N")
                    {
                        NguyenPhuLieu npl = new NguyenPhuLieu();
                        npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        npl.MaHaiQuan = HD.MaHaiQuan;
                        npl.NgayKy = HD.NgayKy;
                        npl.SoHopDong = HD.SoHopDong;
                        NguyenPhuLieuRegistedGCForm f1 = new NguyenPhuLieuRegistedGCForm();
                        f1.NguyenPhuLieuSelected = npl;
                        f1.ShowDialog();
                        if (f1.NguyenPhuLieuSelected.Ma != "")
                        {
                            txtMaHang.Text = npl.Ma;
                            txtTenHang.Text = npl.Ten;
                            txtMaHS.Text = npl.MaHS;
                            cbDonViTinh.SelectedValue = npl.DVT_ID;
                        }
                    }
                    else
                    {
                        ThietBi tb = new ThietBi();
                        tb.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        tb.MaHaiQuan = HD.MaHaiQuan;
                        tb.NgayKy = HD.NgayKy;
                        tb.SoHopDong = HD.SoHopDong;
                        ThietBiRegistedForm ftb = new ThietBiRegistedForm();
                        ftb.ThietBiSelected = tb;
                        ftb.ShowDialog();
                        if (ftb.ThietBiSelected.Ma != "")
                        {
                            txtMaHang.Text = tb.Ma;
                            txtTenHang.Text = tb.Ten;
                            txtMaHS.Text = tb.MaHS;
                            cbDonViTinh.SelectedValue = tb.DVT_ID;
                        }
                    }
                    break;
                case "XGC":
                    if (this.LoaiHangHoa=="N")
                    {
                        NguyenPhuLieu npl = new NguyenPhuLieu();
                        npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        npl.MaHaiQuan = HD.MaHaiQuan;
                        npl.NgayKy = HD.NgayKy;
                        npl.SoHopDong = HD.SoHopDong;
                        NguyenPhuLieuRegistedGCForm f2 = new NguyenPhuLieuRegistedGCForm();
                        f2.NguyenPhuLieuSelected = npl;
                        f2.ShowDialog();
                        if (f2.NguyenPhuLieuSelected.Ma != "")
                        {
                            txtMaHang.Text = npl.Ma;
                            txtTenHang.Text = npl.Ten;
                            txtMaHS.Text = npl.MaHS;
                            cbDonViTinh.SelectedValue = npl.DVT_ID;
                        }
                    }
                    else if (LoaiHangHoa=="S")
                    {
                        SanPham sp = new SanPham();
                        sp.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        sp.MaHaiQuan = HD.MaHaiQuan;
                        sp.NgayKy = HD.NgayKy;
                        sp.SoHopDong = HD.SoHopDong;
                        SanPhamRegistedGCForm f3 = new SanPhamRegistedGCForm();
                        f3.SanPhamSelected = sp;
                        f3.ShowDialog();
                        if (f3.SanPhamSelected.Ma != "")
                        {
                            txtMaHang.Text = sp.Ma;
                            txtTenHang.Text = sp.Ten;
                            txtMaHS.Text = sp.MaHS;
                            cbDonViTinh.SelectedValue = sp.DVT_ID;
                        }
                    }
                    else
                    {
                        ThietBi tb = new ThietBi();
                        tb.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                        tb.MaHaiQuan = HD.MaHaiQuan;
                        tb.NgayKy = HD.NgayKy;
                        tb.SoHopDong = HD.SoHopDong;
                        ThietBiRegistedForm ftb = new ThietBiRegistedForm();
                        ftb.ThietBiSelected = tb;
                        ftb.ShowDialog();
                        if (ftb.ThietBiSelected.Ma != "")
                        {
                            txtMaHang.Text = tb.Ma;
                            txtTenHang.Text = tb.Ten;
                            txtMaHS.Text = tb.MaHS;
                            cbDonViTinh.SelectedValue = tb.DVT_ID;
                        }
                    }
                    break;
            }
        }

        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            GridEXSelectedItemCollection items = dgList.SelectedItems;
            foreach (GridEXSelectedItem i in items)
            {
                if (i.RowType == RowType.Record)
                {                   
                    this.HMD = this.HMDCollection[i.Position];
                    if (HMD != null && HMD.SoToKhai > 0)
                    {
                        txtMaHang.Text = this.HMD.MaHang;
                        txtTenHang.Text = this.HMD.TenHang;
                        txtMaHS.Text = this.HMD.MaHS;
                        cbDonViTinh.SelectedValue = this.HMD.DVT_ID;


                        txtDGNT.Value = this.dgnt =Convert.ToDecimal(this.HMD.DGIA_KB);
                        txtLuong.Value = this.luong = this.HMD.Luong;
                        txtTGNT.Value = this.HMD.TGKB_VND;
                        txtTS_NK.Value = this.ts_nk = this.HMD.TS_XNK;
                        txtTS_TTDB.Value = this.ts_ttdb = this.HMD.TS_TTDB;
                        txtTS_GTGT.Value = this.ts_gtgt = this.HMD.TS_VAT;
                        txtTL_CLG.Value = this.tl_clg = this.HMD.TYLE_THUKHAC;

                        this.ts_nk = this.HMD.TS_XNK / 100;
                        this.ts_ttdb = this.HMD.TS_TTDB / 100;
                        this.ts_gtgt = this.HMD.TS_VAT / 100;
                        this.tl_clg = this.HMD.TYLE_THUKHAC / 100;
                        this.tinhthue();
                    }
                    
                }
                break;
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            HangMauDichDieuChinh hdc = new HangMauDichDieuChinh();
            hdc.MaHaiQuan = tokhai.MaHaiQuan;
            hdc.MaLoaiHinh = tokhai.MaLoaiHinh;
            hdc.NamDangKy = tokhai.NamDangKy;
            hdc.SoToKhai = tokhai.SoToKhai;
            hdc.LanDieuChinh = Convert.ToInt32(txtLanDC.Text);
            HMDCollection = hdc.SelectCollectionBy_SoToKhai_AND_MaLoaiHinh_AND_MaHaiQuan_AND_NamDangKy_AND_LanDieuChinh();
            if (HMDCollection == null || HMDCollection.Count == 0)
            {
                ShowMessage("Tờ khai này không có lần điều chỉnh này.", false);                
            }
            dgList.DataSource = HMDCollection;
            dgList.Refetch();
        }
    }
}
